//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "EnergyBasisSwap.h"
using namespace Cephei::QL::Experimental::Commodities;
#include <gen/QL/Experimental/Commodities/CommodityUnitCost.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Experimental/Commodities/CommodityIndex.h>
#include <gen/QL/Currency.h>
#include <gen/QL/Experimental/Commodities/PricingPeriod.h>
#include <gen/QL/Experimental/Commodities/CommodityType.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Experimental/Commodities/Quantity.h>
#include <gen/QL/Experimental/Commodities/PricingError.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Experimental/Commodities/EnergySwap.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
using namespace Cephei::QL::Termstructures;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Commodities::CEnergyBasisSwap::CEnergyBasisSwap (boost::shared_ptr<QuantLib::EnergyBasisSwap>& childNative, Object^ owner) : CEnergySwap(CEnergyBasisSwap::typeid)
{
#ifdef HANDLE
	_phEnergyBasisSwap = NULL;
#endif
	_ppEnergyBasisSwap = &childNative;
    _ppEnergySwap = new boost::shared_ptr<QuantLib::EnergySwap> (boost::dynamic_pointer_cast<QuantLib::EnergySwap> (*_ppEnergyBasisSwap));
}
Cephei::QL::Experimental::Commodities::CEnergyBasisSwap::CEnergyBasisSwap (QuantLib::EnergyBasisSwap& childNative, Object^ owner) : CEnergySwap(CEnergyBasisSwap::typeid)
{
#ifdef HANDLE
	_phEnergyBasisSwap = NULL;
#endif
	_ppEnergyBasisSwap = new boost::shared_ptr<QuantLib::EnergyBasisSwap> (&childNative);
    _ppEnergySwap = new boost::shared_ptr<QuantLib::EnergySwap> (boost::dynamic_pointer_cast<QuantLib::EnergySwap> (*_ppEnergyBasisSwap));
    _EnergyBasisSwapOwner = owner;
    _EnergySwapOwner = owner;
}

Cephei::QL::Experimental::Commodities::CEnergyBasisSwap::CEnergyBasisSwap (CEnergyBasisSwap^ copy) : CEnergySwap(CEnergyBasisSwap::typeid)
{
#ifdef HANDLE
	_phEnergyBasisSwap = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppEnergyBasisSwap = new boost::shared_ptr<QuantLib::EnergyBasisSwap> (copy->GetShared());
        _ppEnergySwap = new boost::shared_ptr<QuantLib::EnergySwap> (boost::dynamic_pointer_cast<QuantLib::EnergySwap> (*_ppEnergyBasisSwap));
    }
}
Cephei::QL::Experimental::Commodities::CEnergyBasisSwap::CEnergyBasisSwap (PLATFORM::Type^ t) : CEnergySwap(CEnergyBasisSwap::typeid)
{
#ifdef HANDLE
	_phEnergyBasisSwap = NULL;
#endif
	if (!t->IsSubclassOf(CEnergyBasisSwap::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Commodities::CEnergyBasisSwap::CEnergyBasisSwap (QuantLib::Handle<QuantLib::EnergyBasisSwap>& childNative, Object^ owner)  : CEnergySwap(CEnergyBasisSwap::typeid)
{
	_phEnergyBasisSwap = &childNative;
	_ppEnergyBasisSwap = &static_cast<boost::shared_ptr<QuantLib::EnergyBasisSwap>>(childNative.currentLink());
    _ppEnergySwap = new boost::shared_ptr<QuantLib::EnergySwap> (boost::dynamic_pointer_cast<QuantLib::EnergySwap> (*_ppEnergyBasisSwap));
    _EnergyBasisSwapOwner = owner;
}
Cephei::QL::Experimental::Commodities::CEnergyBasisSwap::CEnergyBasisSwap (QuantLib::Handle<QuantLib::EnergyBasisSwap> childNative)  : CEnergySwap(CEnergyBasisSwap::typeid)
{
	_phEnergyBasisSwap = &childNative;
	_ppEnergyBasisSwap = &static_cast<boost::shared_ptr<QuantLib::EnergyBasisSwap>>(childNative.currentLink());
    _ppEnergySwap = new boost::shared_ptr<QuantLib::EnergySwap> (boost::dynamic_pointer_cast<QuantLib::EnergySwap> (*_ppEnergyBasisSwap));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Commodities::CEnergyBasisSwap::CEnergyBasisSwap (QuantLib::EnergyBasisSwap childNative)  : CEnergySwap(CEnergyBasisSwap::typeid)
{
#ifdef HANDLE
	_phEnergyBasisSwap = NULL;
#endif
	_ppEnergyBasisSwap = new boost::shared_ptr<QuantLib::EnergyBasisSwap> (new QuantLib::EnergyBasisSwap (childNative));
    _ppEnergySwap = new boost::shared_ptr<QuantLib::EnergySwap> (boost::dynamic_pointer_cast<QuantLib::EnergySwap> (*_ppEnergyBasisSwap));
}
#endif

Cephei::QL::Experimental::Commodities::CEnergyBasisSwap::~CEnergyBasisSwap ()
{
    if (_ppEnergyBasisSwap != NULL)
    {
	    delete _ppEnergyBasisSwap;
        _ppEnergyBasisSwap = NULL;
    }
}
Cephei::QL::Experimental::Commodities::CEnergyBasisSwap::!CEnergyBasisSwap ()
{
    if (_ppEnergyBasisSwap != NULL)
    {
	    delete _ppEnergyBasisSwap;
    }
}
QuantLib::EnergyBasisSwap& Cephei::QL::Experimental::Commodities::CEnergyBasisSwap::GetReference ()
{
    if (_ppEnergyBasisSwap == NULL) throw REFNEW NativeNullException ();
	return **_ppEnergyBasisSwap;
}
boost::shared_ptr<QuantLib::EnergyBasisSwap>& Cephei::QL::Experimental::Commodities::CEnergyBasisSwap::GetShared ()
{
    if (_ppEnergyBasisSwap == NULL) throw REFNEW NativeNullException ();
	return *_ppEnergyBasisSwap;
}
QuantLib::EnergyBasisSwap* Cephei::QL::Experimental::Commodities::CEnergyBasisSwap::GetPointer ()
{
    if (_ppEnergyBasisSwap == NULL) throw REFNEW NativeNullException ();
	return &**_ppEnergyBasisSwap;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::EnergyBasisSwap>& Cephei::QL::Experimental::Commodities::CEnergyBasisSwap::GetHandle ()
{
	if (_phEnergyBasisSwap == NULL)
	{
		_phEnergyBasisSwap = new Handle<QuantLib::EnergyBasisSwap> (*_ppEnergyBasisSwap);
	}
	return *_phEnergyBasisSwap;
}
#endif
bool Cephei::QL::Experimental::Commodities::CEnergyBasisSwap::HasNative () 
{
	return (_ppEnergyBasisSwap != NULL);
}

Cephei::QL::Experimental::Commodities::ICommodityUnitCost^ Cephei::QL::Experimental::Commodities::CEnergyBasisSwap::Basis::get ()
{
    try
    {
    	QuantLib::CommodityUnitCost& _rv = (QuantLib::CommodityUnitCost&)(*_ppEnergyBasisSwap)->basis ( );   
        Cephei::QL::Experimental::Commodities::CCommodityUnitCost^ _nrv = REFNEW Cephei::QL::Experimental::Commodities::CCommodityUnitCost (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Commodities::ICommodityIndex^ Cephei::QL::Experimental::Commodities::CEnergyBasisSwap::PayIndex::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::CommodityIndex>& _rv = (boost::shared_ptr<QuantLib::CommodityIndex>&)(*_ppEnergyBasisSwap)->payIndex ( );   
        Cephei::QL::Experimental::Commodities::CCommodityIndex^ _nrv = REFNEW Cephei::QL::Experimental::Commodities::CCommodityIndex (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Commodities::ICommodityIndex^ Cephei::QL::Experimental::Commodities::CEnergyBasisSwap::ReceiveIndex::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::CommodityIndex>& _rv = (boost::shared_ptr<QuantLib::CommodityIndex>&)(*_ppEnergyBasisSwap)->receiveIndex ( );   
        Cephei::QL::Experimental::Commodities::CCommodityIndex^ _nrv = REFNEW Cephei::QL::Experimental::Commodities::CCommodityIndex (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

